<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------

namespace app\api\model\card;


use app\api\model\cart\Cart;
use app\core\base\BaseModel;
use app\dict\sys\FileDict;


use think\model\concern\SoftDelete;


/**
 * 商品模型
 * Class Goods
 * @package app\model\goods
 */
class Card extends BaseModel
{

    use SoftDelete;



    /**
     * 模型名称
     * @var string
     */
    protected $name = 'wms_products';

    /**
     * 定义软删除标记字段.
     * @var string
     */
    protected $deleteTime = 'deleted_at';







    /**
     * 获取封面缩略图（小）
     */
    public function getGoodsCoverThumbSmallAttr($value, $data)
    {
        if (isset($data[ 'thumb' ]) && $data[ 'thumb' ] != '') {
            return get_thumb_images($data[ 'thumb' ], FileDict::SMALL);
        }
        return [];
    }

    /**
     * 获取封面缩略图（中）
     */
    public function getGoodsCoverThumbMidAttr($value, $data)
    {
        if (isset($data[ 'thumb' ]) && $data[ 'thumb' ] != '') {
            return get_thumb_images($data[ 'thumb' ], FileDict::MID);
        }
        return [];
    }

    /**
     * 获取封面缩略图（大）
     */
    public function getGoodsCoverThumbBigAttr($value, $data)
    {
        if (isset($data[ 'thumb' ]) && $data[ 'thumb' ] != '') {
            return get_thumb_images($data[ 'thumb' ], FileDict::BIG);
        }
        return [];
    }

    /**
     * 获取商品图片缩略图（小）
     */
    public function getGoodsImageThumbSmallAttr($value, $data)
    {
        if (isset($data[ 'goods_image' ]) && $data[ 'goods_image' ] != '') {
            $goods_image = explode(',', $data[ 'goods_image' ]);
            $img_arr = [];
            foreach ($goods_image as $k => $v) {
                $img = get_thumb_images($v, FileDict::SMALL);
                if (!empty($img)) {
                    $img_arr[] = $img;
                }
            }
            return $img_arr;
        }
        return [];
    }

    /**
     * 获取商品图片缩略图（中）
     */
    public function getGoodsImageThumbMidAttr($value, $data)
    {
        if (isset($data[ 'goods_image' ]) && $data[ 'goods_image' ] != '') {
            $goods_image = explode(',', $data[ 'goods_image' ]);
            $img_arr = [];
            foreach ($goods_image as $k => $v) {
                $img = get_thumb_images($v, FileDict::MID);
                if (!empty($img)) {
                    $img_arr[] = $img;
                }
            }
            return $img_arr;
        }
        return [];
    }

    /**
     * 获取商品图片缩略图（大）
     */
    public function getGoodsImageThumbBigAttr($value, $data)
    {
        if (isset($data[ 'goods_image' ]) && $data[ 'goods_image' ] != '') {
            $goods_image = explode(',', $data[ 'goods_image' ]);
            $img_arr = [];
            foreach ($goods_image as $k => $v) {
                $img = get_thumb_images($v, FileDict::BIG);
                if (!empty($img)) {
                    $img_arr[] = $img;
                }
            }
            return $img_arr;
        }
        return [];
    }

    /**
     * 获取商品分类
     */
    public function getGoodsCategoryAttr($value, $data)
    {
        if (!empty($value)) {
            return array_map(function($item) {
                return (int) $item;
            }, $value);
        }
        return [];
    }

    /**
     * 搜索器:商品商品id
     * @param $value
     * @param $data
     */
    public function searchGoodsIdAttr($query, $value, $data)
    {
        if ($value) {
            $query->where("id", $value);
        }
    }

    /**
     * 搜索器:商品商品名称
     * @param $value
     * @param $data
     */
    public function searchGoodsNameAttr($query, $value, $data)
    {
        if ($value) {
            $query->where("title", "like", "%" . $value . "%");
        }
    }

    /**
     * 搜索器:商品副标题
     * @param $value
     * @param $data
     */
    public function searchSubTitleAttr($query, $value, $data)
    {
        if ($value) {
            $query->where("sub_title", "like", "%" . $value . "%");
        }
    }

    public function searchRarityAttr($query, $value, $data){
        if ($value) {
            $query->where("rarity", "=", $value);
        }
    }





    /**
     * 搜索器:商品商品分类
     * @param $value
     * @param $data
     */
    public function searchGoodsCategoryAttr($query, $value, $data)
    {
        if ($value) {
            if (gettype($value) == 'array') {
                $like_arr = [];
                foreach ($value as $k => $v) {
                    $like_arr[] = $v;
                    $like_arr[] = $v . ',%';
                    $like_arr[] = '%,' . $v . ',%';
                    $like_arr[] = '%,' . $v;
                }
                $query->where("category_ids", "like", $like_arr, 'or');
            } else {
                $like_arr = [
                    $value ,
                    $value . ',%',
                    '%,' . $value . ',%',
                    '%,' . $value
                ];
                $query->where("category_ids", "like", $like_arr, 'or');
            }
        }
    }

    /**
     * 搜索器:商品标签组
     * @param $value
     * @param $data
     */
    public function searchTagsAttr($query, $value, $data)
    {
        if ($value) {
            if (gettype($value) == 'array') {
                $like_arr = [];
                foreach ($value as $k => $v) {
                    $like_arr[] = $v;
                    $like_arr[] = $v . ',%';
                    $like_arr[] = '%,' . $v . ',%';
                    $like_arr[] = '%,' . $v;
                }
                $query->where("tags", "like", $like_arr, 'or');
            } else {
                $like_arr = [
                    $value,
                    $value . ',%',
                    '%,' . $value . ',%',
                    '%,' . $value
                ];
                $query->where("tags", "like", $like_arr, 'or');
            }
        }
    }





    /**
     * 搜索器:商品商品状态（1.正常0下架）
     * @param $value
     * @param $data
     */
    public function searchStatusAttr($query, $value, $data)
    {
        if ($value !== '') {
            $query->where("status", $value);
        }
    }



    /**
     * 关联默认商品规格
     * @return \think\model\relation\HasOne
     */
    public function goodsSku()
    {
        return $this->hasOne(CardSku::class, 'product_id', 'id');
    }

    /**
     * 关联商品规格列表
     * @return \think\model\relation\HasMany
     */
    public function skuList()
    {
        return $this->hasMany(CardSku::class, 'product_id', 'id');
    }








}
